#include "jeux.h"
void jouer (SDL_Surface *screen)
{
    SDL_Surface *vide=NULL,*haut=NULL,*gauche=NULL,*bas=NULL,*droite=NULL;
    SDL_Surface *vide_pion=NULL,*haut_pion=NULL,*gauche_pion=NULL,*bas_pion=NULL,*droite_pion=NULL;
    SDL_Rect pos_blit,pos_pion;
    SDL_Event event;
    int i,j;
    int grille [nbre_case][nbre_case]={0};
    /*chargement des surfaces */
    vide=SDL_LoadBMP("image/vide.bmp");
    haut=SDL_LoadBMP("image/haut.bmp");
    gauche=SDL_LoadBMP("image/gauche.bmp");
    bas=SDL_LoadBMP("image/bas.bmp");
    droite=SDL_LoadBMP("image/droite.bmp");
    vide_pion=SDL_LoadBMP("image/vide_pion.bmp");
    haut_pion=SDL_LoadBMP("image/haut_pion.bmp");
    gauche_pion=SDL_LoadBMP("image/gauche_pion.bmp");
    bas_pion=SDL_LoadBMP("image/bas_pion.bmp");
    droite_pion=SDL_LoadBMP("image/droite_pion.bmp");

    //direction


if (!charger_niveau(grille))
    exit(EXIT_FAILURE);
    //pion a la premiere case

switch(grille[0][0])
    {
        pos_blit.x=0;
        pos_blit.y=0;
        case VIDE:
            SDL_BlitSurface(vide_pion,NULL,screen,&pos_blit);
            break;
        case HAUT: SDL_BlitSurface(haut_pion,NULL,screen,&pos_blit);break;
        case GAUCHE: SDL_BlitSurface(gauche_pion,NULL,screen,&pos_blit);break;
        case BAS: SDL_BlitSurface(bas_pion,NULL,screen,&pos_blit);break;
        case DROITE: SDL_BlitSurface(droite_pion,NULL,screen,&pos_blit);break;
    }

int done=1 ;
while(done)
{
    SDL_PollEvent(&event);
    switch (event.type)
    {
        case SDL_QUIT:done=0; break;
        case SDL_MOUSEBUTTONDOWN: done=0;break;
        case SDL_KEYDOWN :
            switch (event.key.keysym.sym)
            {
                case SDLK_UP: deplacer_pion(grille,pos_pion,HAUT); break;
                case SDLK_LEFT: deplacer_pion(grille,pos_pion,GAUCHE); break;
                case SDLK_DOWN: deplacer_pion(grille,pos_pion,BAS); break;
                case SDLK_RIGHT: deplacer_pion(grille,pos_pion,DROITE); break;
                case SDLK_ESCAPE: done=0;
            }
            break;
    }
    SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,255,255,255));
    for(i=0;i<nbre_case-1;i++)

        {
            for(j=0;j<nbre_case-1;j++)
                {
                pos_blit.x=i*taille_case;
                pos_blit.y=j*taille_case;
                switch(grille[i][j])
                {
                    case VIDE: SDL_BlitSurface(vide,NULL,screen,&pos_blit);break;
                    case HAUT: SDL_BlitSurface(haut,NULL,screen,&pos_blit);break;
                    case GAUCHE: SDL_BlitSurface(gauche,NULL,screen,&pos_blit);break;
                    case BAS: SDL_BlitSurface(bas,NULL,screen,&pos_blit);break;
                    case DROITE: SDL_BlitSurface(droite,NULL,screen,&pos_blit);break;
                }
                }
        }

    SDL_Flip (screen);  //actualiser l'ecran
}
//vider les surfaces
SDL_FreeSurface(vide);
SDL_FreeSurface(haut);
SDL_FreeSurface(gauche);
SDL_FreeSurface(bas);
SDL_FreeSurface(droite);
SDL_FreeSurface(vide_pion);
SDL_FreeSurface(haut_pion);
SDL_FreeSurface(gauche_pion);
SDL_FreeSurface(bas_pion);
SDL_FreeSurface(droite_pion);

}
void deplacer_pion(int grille[][nbre_case],SDL_Rect pos_pion,int direction)
{
    int x,y;
    x=pos_pion.x/50;
    y=pos_pion.y/50;
    switch (direction)
    {
        case HAUT: if ((grille[x][y]!=HAUT_PION)&&(grille[x][y-1]!=BAS)&&(pos_pion.y>0)) {pos_pion.y=y-1; grille[x][y]-=5; grille [x][y-1]+=5;}break;
        case BAS: if ((grille[x][y]!=BAS_PION)&&(grille[x][y-1]!=HAUT)&&(pos_pion.y<hauteur_fenetre)) {pos_pion.y=y+1; grille[x][y]-=5; grille [x][y+1]+=5;}break;
        case DROITE: if ((grille[x][y]!=DROITE_PION)&&(grille[x][y-1]!=GAUCHE)&&(pos_pion.x<largeur_fenetre)) {pos_pion.x=x+1; grille[x][y]-=5; grille [x+1][y]+=5;}break;
        case GAUCHE: if ((grille[x][y]!=GAUCHE_PION)&&(grille[x][y-1]!=DROITE)&&(pos_pion.x>0)) {pos_pion.x=x-1; grille[x][y]-=5; grille [x-1][y]+=5;}break;
    }
}
